import pygame
import random
import os
from pygame.locals import *
from math import *
from defs import *

class Mason(pygame.sprite.Sprite):

   colour = RED
   #colour = TRANSPARENT

   turn_speed = 5.0
   max_speed = 4.0000
   # draw base image
   base_image = pygame.Surface((20, 20))
   base_image.fill(TRANSPARENT)
   base_image.set_colorkey(TRANSPARENT)
   pointlist = [(0,0), (0,20), (20,20), (20, 0)]
   pygame.draw.polygon(base_image, colour, pointlist)
	
   def __init__(self):
      pygame.sprite.Sprite.__init__(self)
      self.image = self.base_image.copy()
      
      self.rect = self.image.get_rect(center=SCREENRECT.center)

      self.vx = 1.0
      self.vy = 0.0
      self.angle = 0
      a = radians(self.angle)
      #self.vx = self.max_speed * cos(a)
      #self.vy = self.max_speed * sin(a)
      self.vx = round(self.max_speed * cos(a))
      self.vy = round(self.max_speed * sin(a))

   def update(self):
      # move the sprite
      #print self.vx
      #print self.vy
      #print '--------------'
      self.rect.move_ip(self.vx, self.vy)
      #self.rect.center = (round(self.rect.centerx + self.vx), round(self.rect.centery + self.vy))
      self.wrap()

   def wrap(self):
      # wrap around the edges of the screen
      if (self.rect.center[0] < 0):
         self.rect.move_ip(SCREENRECT.width, 0)
      elif (self.rect.center[0] >= SCREENRECT.width):
         self.rect.move_ip(-SCREENRECT.width, 0)            

      if (self.rect.center[1] <  FONT_SIZE):
         self.rect.move_ip(0, SCREENRECT.height - FONT_SIZE)
      elif (self.rect.center[1] >= SCREENRECT.height):
         self.rect.move_ip(0, -SCREENRECT.height + FONT_SIZE)

   def turn(self, direction):
      self.angle += self.turn_speed * direction
      if (self.angle < 0):
         self.angle += 360
      if (self.angle >= 360):
         self.angle -= 360
         
      a = radians(self.angle)
      self.vx = self.max_speed * cos(a)
      self.vy = self.max_speed * sin(a)
      '''tempvx = self.max_speed * cos(a)
      tempvy = self.max_speed * sin(a)
      if (round(tempvx) != self.vx and round(tempvy) != self.vy):
         self.vx = tempvx
         self.vy = tempvy
         self.image = pygame.transform.rotate(self.base_image, -self.angle)
         self.rect = self.image.get_rect(center=self.rect.center)'''

      # Note: rotate() takes an angle in degrees
      self.image = pygame.transform.rotate(self.base_image, -self.angle)
      self.rect = self.image.get_rect(center=self.rect.center)
      #self.rect = self.image.get_rect(center=(round(self.rect.centerx),round(self.rect.centery)))
      
   def newXY(self, x, y):
      self.rect = self.image.get_rect(center=(x,y))
